package com.apps.ftapp.activity;


import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.SubjectNewAdapter;
import com.apps.ftapp.model.SubjectModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.SharedPreferencesUtil;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cz.msebera.android.httpclient.Header;

public class NewSubjectActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.listView)
    ListView listView;
    Unbinder unbinder;
    private List<SubjectModel.CategoriesBean> dataList = new ArrayList<>();
    private SubjectNewAdapter subjectAdapter;

    private String yiji_name;

    private Gson gson = new Gson();
    private String week_time = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_subject);
        unbinder = ButterKnife.bind(this);
        initView();
        setTitle("本周新题");
        loadData();

    }


    private void initView() {

        yiji_name = SharedPreferencesUtil.getData(NewSubjectActivity.this, "yijiName", "") + "";
        L.i("main", "yiji_name>>>>" + yiji_name);
//        moreSubject.setOnClickListener(this);


        subjectAdapter = new SubjectNewAdapter(NewSubjectActivity.this, dataList, yiji_name);
        listView.setAdapter(subjectAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                SubjectModel.CategoriesBean temp = (SubjectModel.CategoriesBean) adapterView.getAdapter().getItem(i);

                HashMap<String, Object> data = new HashMap<String, Object>();
                if (!week_time.equals("")) {
                    data.put("time", week_time);
                }
                data.put("id", temp.getId());
                data.put("keys", "");
                data.put("keycur", 0);
                data.put("lmname", temp.getName());

                ActivitySkipUtil.skipAnotherActivity(NewSubjectActivity.this, NewSubjectZdActivity.class, data);

            }
        });

//        refreshLayout.autoRefresh();
//        refreshLayout.finishLoadmore(0,false);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.more_subject:

                // ActivitySkipUtil.skipAnotherActivity(getActivity(), SubjectAddActivity.class,false);
                //  ActivitySkipUtil.skipAnotherActivity(getActivity(), LoginActivity.class,false);
                ActivitySkipUtil.skipAnotherActivity(NewSubjectActivity.this, SubjectMoreActivity.class, false);
                break;

            case R.id.cus_title:
                Constant.IS_EDIT_CHANAGE = "SubjectFragment";
                ActivitySkipUtil.skipAnotherActivity(NewSubjectActivity.this, SubjectMoreActivity.class, false);
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    private void loadData() {

        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("page", 1);
        params.put("week", 1);
        Async.post("tiku/category", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    //SubjectModel
                    try {
                        SubjectModel result = gson.fromJson(new String(responseBody), SubjectModel.class);
                        if (1 == Tools.isIntNull(result.getSuccess())) {
                            if (null != result.getCategories() && result.getCategories().size() > 0) {
                                week_time = result.getWeektime();
                                List<SubjectModel.CategoriesBean> temp = new ArrayList<>();

                                for (int i = 0; i < result.getCategories().size(); i++) {
                                    if (!result.getCategories().get(i).getQuestions().equals("0")) {
                                        temp.add(result.getCategories().get(i));
                                    }
                                }
                                dataList.addAll(temp);
                                subjectAdapter.addRes(temp);

                            }
                        }
                    } catch (Exception e) {

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }

}
